let requestUrl;
/**
 * 宝石镶嵌状态
 */
let stateArray = ["未镶嵌", "镶嵌中"];

function addHandler(){
	let roleId = $("#role_id").combobox("getValue");

	if (roleId) {
		$("#roleId").combobox("setValue", roleId);
	}

	requestUrl = "/baoshi/insert";

	$("#baoshi_dialog").dialog("open");
}

function editHandler() {
	let rowData = $("#baoshi_list").datagrid("getSelected");
	
	if (rowData) {
		requestUrl = "/baoshi/updateById";

		$("#id").val(rowData.id);
		$("#roleId").combobox("setValue", rowData.roleId);
		$("#grade").numberspinner("setValue", rowData.grade);
		$("#categoryId").combobox("setValue", rowData.categoryId);

		$("#baoshi_dialog").dialog("open");
	} else {
		alertMsg("请选择要修改的记录！", "info");
	}
}

function deleteHandler() {
	let rowData = $("#baoshi_list").datagrid("getSelected");

	if (rowData) {
		$.messager.confirm("提示", "删除后数据无法恢复，是否确认删除？", function(bool) {
			if (bool) {
				ajaxPost("/baoshi/deleteById", {
					id: rowData.id
				}, function(resp) {
					showMsg(resp.message);

					$("#baoshi_list").datagrid("reload");
				}, error);
			}
		});
	} else {
		alertMsg("请选择要删除的数据！", "warning");
	}
}

$(document).ready(function() {

	$("#role_id").combobox({
		url: "/role_account/selectAll",
		valueField: "id",
		textField: "name",
		width: 150,
		prompt: "-选择角色-",
		panelHeight: "auto",
		formatter: function(row) {
			ajaxGet("/role_category/selectById", {
				id: row.categoryId
			}, function(resp) {
				let data = resp.data;

				$("#modeling_" + row.id).attr("src", data.image);
			}, error);

			return "<img height='18' id='modeling_" + row.id + "' />&nbsp;" + row.name;
		}
	});

	$("#category_id").combobox({
		url: "/baoshi_category/selectAll",
		valueField: "id",
		textField: "name",
		width: 150,
		prompt: "-宝石类型-",
		panelHeight: "auto",
		formatter: function(row) {
			let text = "";

			text += "<table>";
			text += 	"<tr>"
			text += 		"<td>"
			text += 			"<img width='20' src='" + row.image + "' />";
			text += 		"</td>";

			text += 		"<td>";
			text += 			"<span style='font-size:18px;'>" + row.name + "</span>";
			text += 		"</td>";
			text += 	"</tr>";
			text += "</table>";

			return text;
		}
	});

	$("#search").linkbutton({
		text: "搜索",
		iconCls: "icon-search"
	}).click(function() {
		let categoryId = $("#category_id").combobox("getValue");
		let roleId = $("#role_id").combobox("getValue");

		$("#baoshi_list").datagrid("reload", {
			categoryId: categoryId,
			roleId: roleId
		});
	});
	
	$("#clear").linkbutton({
		text: "清空",
		iconCls: "icon-delete"
	}).click(function() {
		$("#search_form").form("clear");
	});

	/******************************************************************************************************/

	$("#grade").numberspinner({
		min: 1,
		max: 13,
		width: 150,
		required: true
	});

	$("#view").hide();

	$("#categoryId").combobox({
		url: "/baoshi_category/selectAll",
		valueField: "id",
		textField: "name",
		width: 150,
		required: true,
		panelHeight: "auto",
		formatter: function(row) {
			let text = "";

			text += "<table>";
			text += 	"<tr>"
			text += 		"<td>"
			text += 			"<img width='20' src='" + row.image + "' />";
			text += 		"</td>";
			text += 		"<td>";
			text += 			"<div style='font-size:18px;'>" + row.name + "</div>";
			text += 		"</td>";
			text += 	"</tr>";
			text += "</table>";

			return text;
		},
		onSelect: function(record) {
			let grade = $("#grade").numberspinner("getValue");

			if(record && grade) {
				let value = record.propertyValue;

				$("#baoshi_grade").html(grade);
				$("#baoshi_name").html(record.name);
				$("#baoshi_image").attr("src", record.image);

				ajaxGet("/property/selectById", {
					id: record.propertyId
				}, function (resp) {
					let data = resp.data;
					
					$("#property_name").html(data.name);
				}, error);

				$("#baoshi_property").html(grade * value);
				$("#view").show();
			} else {
				$("#view").hide();
			}
		}
	});

	$("#roleId").combobox({
		url: "/role_account/selectAll",
		valueField: "id",
		textField: "name",
		width: 150,
		required: true,
		panelHeight: "auto",
		formatter: function(row) {
			ajaxGet("/role_category/selectById", {
				id: row.categoryId
			}, function(resp) {
				let data = resp.data;

				$("#role_" + row.id).attr("src", data.image);
			}, error);

			return "<img height='20' id='role_" + row.id + "' />&nbsp;" + row.name;
		}
	});

	$("#baoshi_dialog").dialog({
		title: "宝石信息",
		width: 500,
		height: 300,
		buttons: [{
			iconCls: "icon-save",
			text: "保存",
			handler: function() {
				let selector = "#baoshi_form";

				checkForm(selector, function () {
					let data = $(selector).serialize();

					ajaxPost(requestUrl, data, function(resp) {
						showMsg(resp.message);

						$("#view").hide();
						$(selector).form("clear");
						$("#baoshi_dialog").dialog("close");
						$("#baoshi_list").datagrid("reload");
					}, error);
				});
			}
		}, {
			iconCls: "icon-cancel",
			text: "取消",
			handler: function() {
				$("#view").hide();
				$("#baoshi_form").form("clear");
				$("#baoshi_dialog").dialog("close");
			}
		}]
	});

	/******************************************************************************************************/

	/**
	 * 表格体的高度
	 * @type {number}
	 */
	let heigt_body = heigt_tr_with_role_details * 10;
	/**
	 * 表格高度
	 * @type {number}
	 */
	let height_table = heigt_tool + heigt_header + heigt_body + heigt_footer + heigt_scoll_bar + 2;

	$("#baoshi_list").datagrid({
		url: "/baoshi/selectByPage",
		height: height_table,
		fitColumns: true,
		rownumbers: true,
		pagination: true,
		toolbar: [{
			iconCls: "icon-add",
			text: "添加",
			handler: function() {
				addHandler();
			}
		}, "-", {
			iconCls: "icon-edit",
			text: "修改",
			handler: function() {
				editHandler();
			}
		}, "-", {
			iconCls: "icon-delete",
			text: "删除",
			handler: function() {
				deleteHandler();
			}
		}],
		columns: [[
			{field: "categoryId", title: "宝石类型", align: "center", width: 160, sortable: true
				, formatter: function(value, rowData, rowIndex) {
					ajaxGet("/baoshi_category/selectById", {
						id: value
					}, function(resp) {
						let data = resp.data;

						$("#category_" + rowIndex).attr("src", data.image)
							.prop("title", data.name);
					}, error);
					
					return "<img height='" + size + "' id='category_" + rowIndex + "' />";
				}
			},
			{field: "grade", title: "宝石等级", align: "center", width: 160, sortable: true},
			{field: "inlayStatus", title: "镶嵌状态", align: "center", width: 160
				, formatter: function(value) {
					return "<span>" + stateArray[value] + "</span>";
				}
			},
			roleDetailsColumn
		]]
	});

});